package random.fox.p2p.bean;

import java.util.List;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import random.fox.p2p.App;
import random.fox.p2p.AppConfig;
import random.fox.p2p.bean.greendao.DaoMaster;
import random.fox.p2p.bean.greendao.DaoSession;
import random.fox.p2p.bean.greendao.User;
import random.fox.p2p.bean.greendao.UserDao;
import random.fox.p2p.interactors.LoginInteractorImpl;
import random.fox.p2p.interactors.MessageInteractorImpl;
import random.fox.p2p.interactors.RegisterInteractorImpl;
import random.fox.p2p.interactors.UserInfoInteractorImpl;
import random.fox.p2p.ui.MenuView;

@Module(
        injects = {
                RegisterInteractorImpl.class,
                MessageInteractorImpl.class,
                LoginInteractorImpl.class,
                UserInfoInteractorImpl.class,
                MenuView.class               //这个其实不应该这样写的。
        },
        library = true
)
public class BeanModule {


    @Provides
    public DaoMaster provideDaoMaster(App app) {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(app, AppConfig.DB_NAME, null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        return daoMaster;
    }

    @Singleton
    @Provides
    DaoSession provideDaoSession(DaoMaster master) {
        return master.newSession();
    }

    @Singleton
    @Provides
    UserDao provideUserDao(DaoSession session) {
        return session.getUserDao();
    }

    @Singleton
    @Provides
    User provideUser(UserDao dao) {
        List<User> users = dao.loadAll();
        User user;
        if (users == null || users.size() == 0)
            user = new User();
        else
            user = users.get(0);
        return user;
    }

}
